// Ofri Keidar 302933981 group 05

package view.useBeans;

/**
 * Bean class holding customer's data
 */
public class CustomerData {

	// customer details
	String number;
	String name;
	String userName;
	String address;
	String paymentMethod;

	// setters

	/**
	 * Sets customer's number
	 * @param number- number to set 
	 */
	public void setNumber(String number) { this.number = number; }
		
	/**
	 * Sets customer's name
	 * @param name- name to set 
	 */
	public void setName(String name) { this.name = name; }
	
	/**
	 * Sets customer's user-name
	 * @param userName- user-name to set 
	 */
	public void setUserName(String userName) { this.userName = userName; }
		
	/**
	 * Sets customer's address
	 * @param address- address to set 
	 */
	public void setAddress(String address) { this.address = address; }
		
	/**
	 * Sets customer's payment method
	 * @param paymentMethod- payment method to set 
	 */
	public void setPaymentMethod(String paymentMethod) { this.paymentMethod = paymentMethod; }
	
	// getters
	
	/**
	 * Returns customer's number
	 * @return number attribute
	 */
	public String getNumber() {	return number; }
	
	/**
	 * Returns customer's name 
	 * @return name attribute
	 */
	public String getName() { return name; }
	
	/**
	 * Returns customer's user-name 
	 * @return user-name attribute
	 */
	public String getUserName() { return userName; }
	
	/**
	 * Returns customer's address 
	 * @return address attribute
	 */
	public String getAddress() { return address; }
	
	/**
	 * Returns customer's payment method 
	 * @return payment method attribute
	 */
	public String getPaymentMethod() { return paymentMethod; }
	
	// utilities

	/**
	 * Checks if a value for customer number was given,
	 * but data is not completely set
	 * @return True if customer is indicated, False if not
	 */
	public boolean isCustomerIndicated() {
		
		// check if data is set
		if (isDataSet()) { return false; }
		
		// check if a value for number was given
		boolean isNumberGiven = true;
		if (number == null) { isNumberGiven = false; }
		else if (number.equals("")) { isNumberGiven = false; }
		
		// return result
		return isNumberGiven;
	}
	
	/**
	 * Checks if data had been already set.
	 * Data is set if members are not null nor empty strings
	 * @return True if data is set, False if not
	 */
	public boolean isDataSet() {
		
		// validate name
		boolean isNumberSet = true;
		if (number == null) { isNumberSet = false;} 
		else if (number.equals("")) { isNumberSet = false; }
		
		// validate name
		boolean isNameSet = true;
		if (name == null) { isNameSet = false;} 
		else if (name.equals("")) { isNameSet = false; }
		
		// validate user-name
		boolean isUserNameSet = true;
		if (userName == null) { isUserNameSet = false;} 
		else if (userName.equals("")) { isUserNameSet = false; }
		
		// validate address
		boolean isAddressSet = true;
		if (address == null) { isAddressSet = false;} 
		else if (address.equals("")) { isAddressSet = false; }
		
		// validate payment method
		boolean isPaymentMethodSet = true;
		if (paymentMethod == null) { isPaymentMethodSet = false;} 
		else if (paymentMethod.equals("")) { isPaymentMethodSet = false; }
		
		// check if parameters are set
		return isNumberSet && isNameSet && isUserNameSet && isAddressSet
				&& isPaymentMethodSet;
	}
	
	/**
	 * Returns customer number's numeric value.
	 * If data is not set or number string cannot 
	 * be parsed to a positive integer, returns null
	 * @return customer number's numeric value
	 */
	public Integer getCustomerNumberValue() {
		
		// check if received a number
		if (number == null) { return null; }
		if (number.equals("")) { return null; }
		
		// try parsing customer number
		try {
			
			// parse numeric value
			Integer parsedVal = Integer.parseInt(number);
			
			// check success
			if (parsedVal == null) { return null; }
			
			// parsed value is a number, check if positive
			if (parsedVal <= 0) {
				
				// customer number cannot be positive
				return null; 
			}			
			
			// parsed value is a positive integer
			return parsedVal;
			
		} catch (NumberFormatException e) {
			
			// content is not numeric
			return null;
		}
	}
	
	/**
	 * Returns customer number's numeric value.
	 * If data is not set or number string cannot 
	 * be parsed to a positive integer, returns null
	 * @return customer number's numeric value
	 */
	public Integer getCustomerNumberValue(String sourceString) {
		
		// check if received a number
		if (sourceString == null) { return null; }
		if (sourceString.equals("")) { return null; }
		
		// try parsing customer number
		try {
			
			// parse numeric value
			Integer parsedVal = Integer.parseInt(sourceString);
			
			// check success
			if (parsedVal == null) { return null; }
			
			// parsed value is a number, check if positive
			if (parsedVal <= 0) {
				
				// customer number cannot be positive
				return null; 
			}			
			
			// parsed value is a positive integer
			return parsedVal;
			
		} catch (NumberFormatException e) {
			
			// content is not numeric
			return null;
		}
	}
}